package com.lenovo.lcdm.type.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lenovo.lcdm.common.enums.CommonMsgEnum;
import com.lenovo.lcdm.common.enums.DeleteFlagEnum;
import com.lenovo.lcdm.common.exception.BusinessException;
import com.lenovo.lcdm.common.exception.ServiceException;
import com.lenovo.lcdm.type.util.ContextUtil;
import com.lenovo.lcdm.type.dto.DataTypeDTO;
import com.lenovo.lcdm.type.dto.GlobalAttributeDTO;
import com.lenovo.lcdm.type.entity.TypeDataType;
import com.lenovo.lcdm.type.entity.TypeGlobalAttribute;
import com.lenovo.lcdm.type.mapper.TypeGlobalAttributeMapper;
import com.lenovo.lcdm.type.service.TypeDataTypeService;
import com.lenovo.lcdm.type.service.TypeGlobalAttributeService;
import com.lenovo.lcdm.type.service.TypeLocalAttributeService;
import com.lenovo.lcdm.type.util.ObjectUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author xujing30
* @description 针对表【type_global_attribute】的数据库操作Service实现
* @createDate 2024-01-03 16:32:57
*/
@Service
public class TypeGlobalAttributeServiceImpl extends ServiceImpl<TypeGlobalAttributeMapper, TypeGlobalAttribute>
    implements TypeGlobalAttributeService{

    @Autowired
    private TypeDataTypeService dataTypeService;

    @Autowired
    private TypeLocalAttributeService localAttributeService;

    @Override
    public TypeDataType findGlobalAttributeDataType(Long globalAttributeId) {
        TypeGlobalAttribute TypeGlobalAttribute = getById(globalAttributeId);
        if (!ObjectUtil.isAnyEmpty(TypeGlobalAttribute, TypeGlobalAttribute.getDataTypeId())) {
            return dataTypeService.getById(TypeGlobalAttribute.getDataTypeId());
        }
        return null;
    }

    @Override
    public List<TypeGlobalAttribute> findAll() {
        LambdaQueryWrapper<TypeGlobalAttribute> qw = new LambdaQueryWrapper();
        qw.eq(TypeGlobalAttribute::getDeleted, DeleteFlagEnum.NO.getValue());
        return list(qw);
    }

    @Override
    public List<TypeGlobalAttribute> queryByFolderId(Long folderId) {
        LambdaQueryWrapper<TypeGlobalAttribute> qw = new LambdaQueryWrapper();
        qw.eq(TypeGlobalAttribute::getAttributeFolderId, folderId).eq(TypeGlobalAttribute::getDeleted, DeleteFlagEnum.NO.getValue());
        return list(qw);
    }

    @Override
    public List<GlobalAttributeDTO> queryGlobalAttribute(Long attributeFolderId) {
        List<GlobalAttributeDTO> attributeDtoList = new ArrayList<>();
        List<TypeGlobalAttribute> TypeGlobalAttributeList = queryByFolderId(attributeFolderId);

        // 如果有全局属性，组装属性对应的数据类型
        if (!CollectionUtils.isEmpty(TypeGlobalAttributeList)) {
            List<TypeDataType> TypeDataTypeList = dataTypeService.findAll();
            // 将所有数据类型放入Map
            Map<Long, TypeDataType> dataTypeMap = new HashMap<>();
            for (TypeDataType dataType : TypeDataTypeList) {
                dataTypeMap.put(dataType.getId(), dataType);
            }
            GlobalAttributeDTO globalAttributeDto = null;
            for (TypeGlobalAttribute TypeGlobalAttribute : TypeGlobalAttributeList) {
                globalAttributeDto = new GlobalAttributeDTO();

                BeanUtils.copyProperties(TypeGlobalAttribute, globalAttributeDto);

                TypeDataType dataType = dataTypeMap.get(TypeGlobalAttribute.getDataTypeId());
                if (!ObjectUtils.isEmpty(dataType)) {
                    globalAttributeDto.setDataTypeName(dataType.getName());
                }
                attributeDtoList.add(globalAttributeDto);
            }
        }
        return attributeDtoList;
    }

    @Override
    public GlobalAttributeDTO createGlobalAttribute(GlobalAttributeDTO globalAttributeDTO) {
        // 校验名称
        if (validateName(globalAttributeDTO.getName())){
            throw new BusinessException("The attribute name already exists. Please use another attribute name", CommonMsgEnum.FAIL.getCode());
        }
        TypeGlobalAttribute globalAttribute = new TypeGlobalAttribute();
        BeanUtils.copyProperties(globalAttributeDTO, globalAttribute);
        globalAttribute.setCreateTime(LocalDateTime.now());
        globalAttribute.setCreator(ContextUtil.getCurrentUser());
        globalAttribute.setModifyTime(LocalDateTime.now());
        globalAttribute.setModifier(ContextUtil.getCurrentUser());
        globalAttribute.setObjectClass(globalAttribute.getClass().getName());
        saveOrUpdate(globalAttribute);

        GlobalAttributeDTO globalAttributeDto = new GlobalAttributeDTO();
        BeanUtils.copyProperties(globalAttribute, globalAttributeDto);
        return globalAttributeDto;
    }

    @Override
    public void updateGlobalAttribute(GlobalAttributeDTO globalAttributeDTO) {
        TypeGlobalAttribute oldObject = getById(globalAttributeDTO.getId());
        if (ObjectUtils.isEmpty(oldObject)) {
            throw new ServiceException("GlobalAttribute Object is not exist" ,CommonMsgEnum.FAIL.getCode());
        } else {
            TypeGlobalAttribute globalAttribute = new TypeGlobalAttribute();
            BeanUtils.copyProperties(globalAttributeDTO, globalAttribute);
            globalAttribute.setCreator(oldObject.getCreator());
            globalAttribute.setCreateTime(oldObject.getCreateTime());
            globalAttribute.setDeleted(0);
            globalAttribute.setObjectClass(globalAttribute.getClass().getName());
            globalAttribute.setModifier(ContextUtil.getCurrentUser());
            globalAttribute.setModifyTime(LocalDateTime.now());
            saveOrUpdate(globalAttribute);
        }
    }

    @Override
    public void deleteGlobalAttribute(GlobalAttributeDTO globalAttributeDTO) {
        // 判断全局属性是否关联
        if (queryByIdInLocalAttribute(globalAttributeDTO.getId())){
            throw new BusinessException("You cannot delete the attributes which are being used in type definition!", CommonMsgEnum.FAIL.getCode());
        }
        TypeGlobalAttribute globalAttribute = getById(globalAttributeDTO.getId());
        if (!ObjectUtils.isEmpty(globalAttribute)) {
            removeById(globalAttribute.getId());
        } else {
            throw new ServiceException("GlobalAttribute Object is not exist" ,CommonMsgEnum.FAIL.getCode());
        }
    }

    @Override
    public boolean queryByIdInLocalAttribute(Long id) {
        if (CollectionUtils.isEmpty(localAttributeService.queryByGlobalAttributeId(id))){
            return false;
        }
        return true;
    }

    @Override
    public GlobalAttributeDTO getGlobalAttribute(GlobalAttributeDTO globalAttributeDTO) {
        GlobalAttributeDTO dto = new GlobalAttributeDTO();
        return dto;
    }

    @Override
    public List<DataTypeDTO> queryDataType() {
        List<DataTypeDTO> dataTypeDTOList = new ArrayList<>();
        List<TypeDataType> TypeDataTypeList = dataTypeService.findAll();

        DataTypeDTO dataTypeDto = null;
        for (TypeDataType dataType : TypeDataTypeList) {
            dataTypeDto = new DataTypeDTO();
            dataTypeDto.setId(dataType.getId());
            dataTypeDto.setName(dataType.getName());

            dataTypeDTOList.add(dataTypeDto);
        }
        return dataTypeDTOList;
    }

    public boolean validateName(String name) {
        LambdaQueryWrapper<TypeGlobalAttribute> qw = new LambdaQueryWrapper();
        qw.eq(TypeGlobalAttribute::getName, name).eq(TypeGlobalAttribute::getDeleted, DeleteFlagEnum.NO.getValue());
        List nameList = list(qw);
        if (CollectionUtils.isEmpty(nameList)){
            return false;
        }
        return true;
    }



}




